package com.lxmes.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.lxmes.common.core.domain.entity.SysDictData;
import com.lxmes.common.core.domain.entity.SysDictType;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.system.service.ISysDictDataService;
import com.lxmes.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.base.mapper.NcFieldDictMapper;
import com.lxmes.base.domain.NcFieldDict;
import com.lxmes.base.service.INcFieldDictService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 字段下拉定义Service业务层处理
 *
 * @author lx
 * @date 2023-04-18
 */
@Service
public class NcFieldDictServiceImpl implements INcFieldDictService
{
    @Autowired
    private NcFieldDictMapper ncFieldDictMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询字段下拉定义
     *
     * @param id 字段下拉定义主键
     * @return 字段下拉定义
     */
    @Override
    public NcFieldDict selectNcFieldDictById(Long id)
    {
        return ncFieldDictMapper.selectNcFieldDictById(id);
    }

    /**
     * 查询字段下拉定义列表
     *
     * @param ncFieldDict 字段下拉定义
     * @return 字段下拉定义
     */
    @Override
    public List<NcFieldDict> selectNcFieldDictList(NcFieldDict ncFieldDict)
    {
        return ncFieldDictMapper.selectNcFieldDictList(ncFieldDict);
    }
/************将此表同步到字典表的方法  未去重

    @Autowired
    ISysDictTypeService dictTypeService;
    @Autowired
    ISysDictDataService dictDataService;
    @Transactional(rollbackFor = Exception.class)
    public void synchronizeTable() {
        List<NcFieldDict> ncFieldDicts = ncFieldDictMapper.selectNcFieldDictList(null);
        Map<String, List<NcFieldDict>> collect = ncFieldDicts.stream().collect(Collectors.groupingBy(NcFieldDict::getFieldName));
        for (Map.Entry<String, List<NcFieldDict>> entry : collect.entrySet()) {
            List<NcFieldDict> value = entry.getValue();
            SysDictType dictType = new SysDictType();
            NcFieldDict first = value.get(0);
            dictType.setDictName(first.getFieldComment());
            dictType.setGcCode(first.getGcCode());
            dictType.setGcName(first.getGcName());
            dictType.setTableName(first.getTableName());
            dictType.setInterpretation(first.getInterpretation());
            dictType.setDictType(first.getFieldName());
            dictType.setFieldName(first.getFieldComment());
            dictType.setFieldComment(first.getFieldComment());
            dictType.setStatus("0");
            dictType.setCreateTime(new Date());
            dictTypeService.insertDictType(dictType);
            int sort = 1;
            for (NcFieldDict ncFieldDict : value) {
                SysDictData dictData = new SysDictData();
              
                dictData.setDictSort(Convert.toLong(sort++));
                dictData.setDictLabel(ncFieldDict.getFieldChooseName());
                dictData.setDictValue(ncFieldDict.getFieldCode());
                dictData.setDictType(first.getFieldName());
                dictData.setStatus("0");
                dictData.setCreateTime(new Date());
                dictDataService.insertDictData(dictData);
            }
        }
    }
*/

    /**
     * 新增字段下拉定义
     *
     * @param ncFieldDict 字段下拉定义
     * @return 结果
     */
    @Override
    public int insertNcFieldDict(NcFieldDict ncFieldDict)
    {
        ncFieldDict.setCreateTime(DateUtils.getNowDate());
        return ncFieldDictMapper.insertNcFieldDict(ncFieldDict);
    }

    /**
     * 修改字段下拉定义
     *
     * @param ncFieldDict 字段下拉定义
     * @return 结果
     */
    @Override
    public int updateNcFieldDict(NcFieldDict ncFieldDict)
    {
        ncFieldDict.setUpdateTime(DateUtils.getNowDate());
        return ncFieldDictMapper.updateNcFieldDict(ncFieldDict);
    }

    /**
     * 批量删除字段下拉定义
     *
     * @param ids 需要删除的字段下拉定义主键
     * @return 结果
     */
    @Override
    public int deleteNcFieldDictByIds(Long[] ids)
    {
        return ncFieldDictMapper.deleteNcFieldDictByIds(ids);
    }

    /**
     * 删除字段下拉定义信息
     *
     * @param id 字段下拉定义主键
     * @return 结果
     */
    @Override
    public int deleteNcFieldDictById(Long id)
    {
        return ncFieldDictMapper.deleteNcFieldDictById(id);
    }
    @Override
    public String imporIteminfo(List<NcFieldDict> ncFieldDictList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncFieldDictList) || ncFieldDictList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcFieldDict ncFieldDict : ncFieldDictList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncFieldDict);
                ncFieldDict.setCreateBy(operName);
                ncFieldDict.setUpdateBy(operName);
                this.insertNcFieldDict(ncFieldDict);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
